package cn.hotel.servlet;

import cn.hotel.pojo.HouseType;
import cn.hotel.pojo.Member;
import cn.hotel.pojo.RoomKeeper;
import cn.hotel.service.OrderFormService;
import cn.hotel.service.OrderFormServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/*表单验证和Ajax*/
@WebServlet("/formVerify")
public class FormVerifyServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out  = response.getWriter();
        OrderFormService orderFormService = new OrderFormServiceImpl();
        String type = request.getParameter("type");
        switch (type){
            //查空余房间 根据房间类型
            case "roomRates" :
                roomRates(request,response,out, orderFormService);
                break;
                //查空房间价格 根据房间类型
            case "roomPrice" :
                roomPrice(request,response,out,orderFormService);
                break;
                //根据手机号验证手机号是否已存在
            case "phoneVerify" :
                phoneVerify(request,response,out,orderFormService);
                break;
                //根据身份证号验证身份证号是否已存在
            case "idCardVerify" :
                idCardVerify(request,response,out,orderFormService);
                break;
                //修改房间状态归0
            case "setState" :
                setState(request,response,out,orderFormService);
                break;
                //会员打折验证
            case "vip" :
                vip(request,response,out,orderFormService);
                break;
        }
    }

    //会员打折验证
    private void vip(HttpServletRequest request, HttpServletResponse response, PrintWriter out, OrderFormService orderFormService) throws ServletException,IOException{
        String mobilePhone = request.getParameter("mobilePhone");
        Member vip = orderFormService.vip(mobilePhone);
        String result = JSON.toJSONString(vip);
        out.print(result);
        out.flush();
    }

    //修改房间状态归0
    private void setState(HttpServletRequest request, HttpServletResponse response, PrintWriter out, OrderFormService orderFormService) throws ServletException,IOException{
        int roomNumber = 0;
        try {
            roomNumber = Integer.parseInt(request.getParameter("roomNumber"));
        }catch (Exception e){}
        int result = orderFormService.setState(0,roomNumber);
        out.print(result);
        out.flush();
    }

    //根据身份证号验证身份证号是否已存在
    private void idCardVerify(HttpServletRequest request, HttpServletResponse response, PrintWriter out, OrderFormService orderFormService) throws ServletException,IOException{
        String identityCard = request.getParameter("identityCard");
        int result = orderFormService.idCardVerify(identityCard);
        out.print(result);
        out.flush();
    }

    //根据手机号验证手机号是否已存在
    private void phoneVerify(HttpServletRequest request, HttpServletResponse response, PrintWriter out, OrderFormService orderFormService) throws ServletException,IOException{
        String mobilePhone = request.getParameter("mobilePhone");
        int result = orderFormService.phoneVerify(mobilePhone);
        out.print(result);
        out.flush();
    }

    //查空房间价格 根据房间类型
    private void roomPrice(HttpServletRequest request, HttpServletResponse response, PrintWriter out, OrderFormService orderFormService) throws ServletException,IOException{
        //接收房型
        int houseTypeId = 0;
        try {
            houseTypeId = Integer.parseInt(request.getParameter("houseTypeId"));
        }catch (Exception e){}
        //
        HouseType houseType = orderFormService.roomPrice(houseTypeId);
        String result = JSON.toJSONString(houseType);
        out.print(result);
        out.flush();
    }

    //查空余房间 根据房间类型
    private void roomRates(HttpServletRequest request, HttpServletResponse response, PrintWriter out, OrderFormService orderFormService) throws ServletException,IOException{
        //接收房型
        int houseTypeId = 0;
        try {
            houseTypeId = Integer.parseInt(request.getParameter("houseTypeId"));
        }catch (Exception e){}
        //
        List<RoomKeeper> roomKeeper = orderFormService.roomRates(houseTypeId);
        String result = null;
        try {
            result = JSON.toJSONString(roomKeeper);
        }catch (Exception e){
            e.printStackTrace();
        }
        out.print(result);
        out.flush();
    }
}
